import sys

# ----------------------------------------------------
import wx
import wx.aui
import wx.py 
import wx.glcanvas
import wx.lib.flatnotebook as fnb
import math

from types import *
#from time import *
from demoEnginePython import *

#---------------------------------------------------------------------------
def time():
    return v.Time()/1000.0

#---------------------------------------------------------------------------
# a=Animation( 0,3,Vec(0,0,0),Vec(5,0,0), n.SetPosition)

class Animation:
    def __init__(self, t0, len, v1, v2, callback):
        self.startTime = t0
        self.expired = False
        self.t0 = t0 * 1.0
        self.len = len * 1.0
        self.v1 = v1
        self.v2 = v2
        self.callback = callback

    def start(self):
        self.expired = False

    def tick(self, time):
        if( self.expired ): return
        if( time < self.t0 ) :return
        if( time > self.t0+self.len ):
            self.expired = True
            t = 1
        else:
            t = (time-self.t0)/self.len
            t = 0.5 * (1 + math.sin( 3.14159265 * (t-0.5) ))
        v = self.interpolate( self.v1,self.v2,t )
        self.callback(v)

    def interpolate(self, v1,v2,t):
        if type(v1) is FloatType or type(v1) is IntType:
            return v1*(1-t)+v2*t
        else:
            return v1.Mul(1-t).Sum(v2.Mul(t) )
        
        

#---------------------------------------------------------------------------
class AnimManager:
    def __init__(self):
        self.anims = []
        self.running = False
        self.loopmode = False
    
    def clear(self):
        self.anims = []
    
    def add(self, animation):
        self.anims.append(animation)

    def play(self):
        self.running = True
        self.startTime = time()
        self.loopmode = False
        self.t0 = self.startTime
        for a in self.anims:
            a.start()

    def loop(self):
        self.running = True
        self.startTime = time()
        self.t0 = self.startTime
        self.loopmode = True
        for a in self.anims:
            a.start()

    def stop(self):
        self.running = False
        self.loopmode = False
        
    def tick(self):
        if not self.running : return
        t = time() - self.startTime
        dt = t-self.t0
        self.t0 = t
        #print dt

        for a in self.anims:
            a.tick( t )

        expired = True
        for a in self.anims:
            expired = expired and a.expired
        if expired: 
            if self.loopmode :
                self.loop()
            else:
                self.stop()
            
        
    
            
        
#---------------------------------------------------------------------------

class TestFrame(wx.Frame):
    def __init__(self, parent=None, ID=-1, title='pyOSG' ):
        wx.Frame.__init__(self, parent, ID, title)
        
        #-- shell---------------------------------------------
        self.shell = wx.py.shell.Shell(self, -1, introText="",style =wx.NO_BORDER)

        # --- OSG ---------------------------------
        self.am = AnimManager()
        
        self.root = Node()
        print self.root.Load("axes.osg")

        self.node = Node(self.root)
        self.node.Scale(0.1)
        print self.node.Load("cow.osg")

        self.viewerPanel = wx.glcanvas.GLCanvas(self,-1)
        self.viewer = Viewer( self.viewerPanel.GetHandle() )
        self.viewer.SetRoot(self.root)

        globals()['am'] = self.am
        globals()['r'] = self.root
        globals()['n'] = self.node 
        globals()['v'] = self.viewer
        globals()['c'] = self.viewer.GetCamera()
        globals()['m'] = self.viewer.GetManipulator()
        
        a=Animation( 0,1,Vec(0,0,0),Vec(5,0,0), self.node.SetPosition)
        self.am.add( a )

        # --- Timer ---------------------------------
        self.timer = wx.Timer(self,-1)
        self.timer.Start(10) 

        # --- Layout ---------------------------------
        self.SetSize(wx.Size(600,900))
        self._mgr = wx.aui.FrameManager()
        self._mgr.SetManagedWindow(self)
        self._mgr.GetArtProvider().SetColor(wx.aui.AUI_ART_INACTIVE_CAPTION_COLOUR, wx.Color(0,150,255)) 
        self._mgr.GetArtProvider().SetColor(wx.aui.AUI_ART_INACTIVE_CAPTION_GRADIENT_COLOUR, wx.Color(0,80,255)) 
        self._mgr.GetArtProvider().SetColor(wx.aui.AUI_ART_INACTIVE_CAPTION_COLOUR, wx.Color(80,150,255)) 
        self._mgr.GetArtProvider().SetColor(wx.aui.AUI_ART_INACTIVE_CAPTION_GRADIENT_COLOUR, wx.Color(80,150,255)) 

        self._mgr.AddPane(self.viewerPanel, wx.aui.PaneInfo().
                        Name("CenterPane").CenterPane())

        self._mgr.AddPane(self.shell, wx.aui.PaneInfo().
                        Name("shell").Caption("shell").Bottom().
                        BestSize(wx.Size(200,300)).MinSize(wx.Size(200,100)))
        
        self._mgr.Update()

        # --- Bindings ---------------------------------
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Bind(wx.EVT_TIMER, self.OnTimer, id=self.timer.GetId() )
                
    def OnClose(self, event):
        #self.viewer.setSceneData(None)
        self.timer.Stop()
        self.node = None
        self.viewer = None
        self.Destroy()
        
    def OnTimer(self,e):
        self.am.tick()
        self.viewer.Frame()
        
#---------------------------------------------------------------------------

class TestApp(wx.App):
    def OnInit(self):
        self.frame = TestFrame()
        self.SetTopWindow(self.frame)
        self.frame.Show() 
        return True
   
#---------------------------------------------------------------------------

if __name__ == '__main__':
    app = TestApp(0)
    app.MainLoop()
    
    

